﻿var templateSuccess = '<div><a href="#Create\" > Crear otro </a> <a  href="#Info\"">Volver al listado </a> <label>{0}</label>';

function ContactViewModel(model) {
    var selfContact = this;
    ko.mapping.fromJS(model, {}, selfContact);

    selfContact.EmptyEntity = ko.observable(selfContact.Entity);

    selfContact.Main = mainViewModel;

    selfContact.Main.Hashes.push("Info");
    selfContact.Main.Hashes.push("Read");
    selfContact.Main.Hashes.push("Create");
    selfContact.Main.Hashes.push("Update");
    selfContact.Main.Hashes.push("Delete");

    selfContact.Main.CurrentPage("Info");
    Sammy().run();

    selfContact.SelectedItem = ko.observable();

    selfContact.FormEnabling = function (page) {
        if (page == "Read" || page == "Delete")
            whoever.DisableAll();
        else
            whoever.EnableAll();

        whoever.EnableButtons();
    }

    selfContact.SelectItem = function (action, item, args) {
        item.Name.extend({ accountOrEmail: "" });;
        selfContact.SelectedItem(item);
        selfContact.Main.ChangeHash(action + "/" + item.Id());
    }

    selfContact.Main.CurrentPage.subscribe(function (newValue) {
        selfContact.FormEnabling(newValue);
    })

    selfContact.LoadIndexed = function (id) {
        selfContact.SelectedItem(whoever.GetBy(selfContact.List(), "Id", id)[0]);
    }

    selfContact.Read = function () {
    }

    selfContact.FilterList = function () {
        health.ShowLoading();
        $.ajax({
            url: $("#filter").attr("action"),
            type: 'POST',
            data: ko.toJSON(selfContact.Filter),
            contentType: 'application/json',
            success: function (result) {
                ko.mapping.fromJS(result, {}, selfContact.List);
                health.HideLoading();
                $('#filterModal').modal('hide')
            }
        });
    }

    selfContact.Create = function () {
        if (selfContact.Main.IsValidModel(selfContact.Entity)) {
            health.ShowLoading();
            $.ajax({
                url: $("#create").attr("action"),
                type: 'POST',
                data: ko.toJSON(selfContact.Entity),
                contentType: 'application/json',
                success: function (result) {
                    if (!result.Success) {
                        health.HideLoading(function () { health.Message(result.Message, 1) });
                    }
                    else {
                        health.HideLoading(function () {
                            selfContact.Entity = selfContact.EmptyEntity;
                            var mensaje = templateSuccess.replace("{0}", result.Message);
                            health.Message(mensaje, 0, 999999);
                        });
                    }
                }
            });
        }
    }

    selfContact.Update = function () {
        if (selfContact.Main.IsValidModel(selfContact.SelectedItem)) {
            health.ShowLoading();
            $.ajax({
                url: $("#update").attr("action"),
                type: 'POST',
                data: ko.toJSON(selfContact.SelectedItem),
                contentType: 'application/json',
                success: function (result) {
                    if (!result.Success) {
                        health.HideLoading(
                            function ()
                            {
                                health.Message(result.Message, 1)
                            });
                    }
                    else {
                        health.HideLoading(function () {
                                health.Message(result.Message, 0, 999999);
                            });
                    }
                }
            });
        }
    }

    selfContact.Delete = function () {
        if (selfContact.Main.IsValidModel(selfContact.SelectedItem)) {
            health.ShowLoading();
            $.ajax({
                url: $("#delete").attr("action"),
                type: 'POST',
                data: ko.toJSON(selfContact.SelectedItem),
                contentType: 'application/json',
                success: function (result) {
                    if (!result.Success) {
                        health.HideLoading(function () { health.Message(result.Message, 0) });
                    }
                    else
                        health.HideLoading(function () { health.Message(result.Message, 1) });
                }
            });
        }
    }

    selfContact.Init = function () {
        if (selfContact.Main.TryIndexed())
            selfContact.LoadIndexed(selfContact.Main.IndexedId());

        selfContact.FormEnabling(selfContact.Main.CurrentPage());
    }

    selfContact.Init();
}